package cn.lsc.service.impl;

import cn.lsc.doc.courseDoc;
import cn.lsc.dto.CourseSearchDto;
import cn.lsc.repository.courseRepository;
import cn.lsc.result.PageList;
import cn.lsc.service.ISearchService;
import cn.lsc.util.StrUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SearchServiceImpl implements ISearchService {
    @Autowired
    private ElasticsearchRestTemplate template;
    @Autowired
    private courseRepository courseRepository;
    @Override
    public void savechcourse(courseDoc courseDoc) {
        courseRepository.save(courseDoc);
    }

    @Override
    public PageList<courseDoc> searchCourse(CourseSearchDto dto) {

        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //分页条件
        builder.withPageable(PageRequest.of(dto.getPage()-1,dto.getRows()));

        //根据参数进行排序
        if(StringUtils.hasLength(dto.getSortField())){
            String sortField = null;
            switch (dto.getSortField().toLowerCase()){
                case "xl": sortField="saleCount";break;
                case "xp": sortField="onlineTime";break;
                case "pl": sortField="commentCount";break;
                case "jg": sortField="price";break;
                case "rq": sortField="viewCount";break;
            }
            if(StringUtils.hasLength(sortField)){//如果字符序列不为 null 值,并且字符序列的长度大于 0 ,则返回 true
                //使用工具对参数进行排序  如果sortField为null就默认倒叙
                SortOrder desc = SortOrder.DESC;
                //如果sortField不是null就给他升序
                if(StringUtils.hasLength(dto.getSortType()) && dto.getSortType().equalsIgnoreCase(SortOrder.ASC.toString())){
                    desc=SortOrder.ASC;
                }
                builder.withSort(SortBuilders.fieldSort(sortField).order(desc));
            }
        }
        //设置查询条件  使用工具类
        BoolQueryBuilder boolquery = QueryBuilders.boolQuery();
        //对参数使用must模糊查询条件 es种还有许多得查询条件
        if(StringUtils.hasLength(dto.getKeyword())){
            boolquery.must(QueryBuilders.matchQuery("name",dto.getKeyword()));
        }



        //等值查询fillter

        if(dto.getCourseTypeId()!=null){
            boolquery.filter(QueryBuilders.termQuery("courseTypeId",dto.getCourseTypeId()));
        }
        //课程等级
        if(StringUtils.hasLength(dto.getGradeName())){
            boolquery.filter(QueryBuilders.termQuery("gradeName",dto.getGradeName()));
        }
        //收费方式
        if(StringUtils.hasLength(dto.getChargeName())){
            boolquery.filter(QueryBuilders.termQuery("chargeName",dto.getChargeName()));
        }
        //最大价格排序
        if(dto.getPriceMax()!=null){
            boolquery.filter(QueryBuilders.rangeQuery("price").lte(dto.getPriceMax()));
        }
        //最小价格排序
        if(dto.getPriceMin()!=null){
            boolquery.filter(QueryBuilders.rangeQuery("price").gte(dto.getPriceMin()));
        }
        //设置查询条件给builder
        builder.withQuery(boolquery);

        //讲查询条件包装起来 使用builder
        NativeSearchQuery build = builder.build();//获取到构建器，帮我们创建查询对象
        //根据包装好了得属性条件进行查询
        Page<courseDoc> search = courseRepository.search(build);

        return  new PageList<>(search.getTotalElements(),search.getContent());
    }
}
